package org.abc.fund.repository.factorRepository;

import org.abc.fund.entity.factorEntity.FactorTree;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 因子树Repository
 */
@Repository
public interface FactorTreeRepository extends JpaRepository<FactorTree, Long> {

    /**
     * 根据树名称查找因子树
     */
    Optional<FactorTree> findByTreeName(String treeName);

    /**
     * 根据业务场景查找因子树列表
     */
    List<FactorTree> findByBusinessScenario(FactorTree.BusinessScenario businessScenario);

    /**
     * 根据是否启用查找因子树列表
     */
    List<FactorTree> findByEnabled(Boolean enabled);

    /**
     * 统计启用的因子树数量
     */
    long countByEnabledTrue();

    /**
     * 根据业务场景和是否启用查找因子树列表
     */
    List<FactorTree> findByBusinessScenarioAndEnabled(FactorTree.BusinessScenario businessScenario, Boolean enabled);

    /**
     * 根据树名称模糊查询
     */
    @Query("SELECT ft FROM FactorTree ft WHERE ft.treeName LIKE %:treeName%")
    List<FactorTree> findByTreeNameContaining(@Param("treeName") String treeName);

    /**
     * 分页查询因子树
     */
    Page<FactorTree> findByBusinessScenarioAndEnabled(FactorTree.BusinessScenario businessScenario, Boolean enabled, Pageable pageable);

    /**
     * 检查树名称是否存在
     */
    boolean existsByTreeName(String treeName);

    /**
     * 根据排序查找因子树列表
     */
    List<FactorTree> findByEnabledOrderBySortOrderAsc(Boolean enabled);
}